﻿package mortal.game.resource.tableConfig
{
    import Message.DB.Tables.*;
    import __AS3__.vec.*;
    import com.gengine.resource.*;
    import com.gengine.utils.*;
    import flash.utils.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.view.common.*;
    import mortal.game.view.levelReward.other.*;

    public class LevelRewardConfig extends Object
    {
        private var _rewardDic:Dictionary;
        private var _giftDic:Dictionary;
        private var maxCount:int = 0;
        private static var _instance:LevelRewardConfig;

        public function LevelRewardConfig()
        {
            if (_instance != null)
            {
                throw new Error(" LevelRewardConfig 单例 ");
            }
            this.init();
            return;
        }// end function

        public function init() : void
        {
            var _loc_1:* = ConfigManager.instance.getJSONByFileName("t_level_reward_config.json");
            this.write(_loc_1);
            return;
        }// end function

        private function write(param1:Object) : void
        {
            var _loc_2:TLevelRewardConfig = null;
            var _loc_3:Object = null;
            if (!this._rewardDic)
            {
                this._rewardDic = new Dictionary();
            }
            for each (_loc_3 in param1)
            {
                
                _loc_2 = new TLevelRewardConfig();
                ClassTypesUtil.copyValue(_loc_2, _loc_3);
                this._rewardDic[_loc_2.level] = _loc_2;
                var _loc_6:String = this;
                var _loc_7:* = this.maxCount + 1;
                _loc_6.maxCount = _loc_7;
            }
            return;
        }// end function

        private function getCodeByLevel(param1:int) : TLevelRewardConfig
        {
            return this._rewardDic[param1] as TLevelRewardConfig;
        }// end function

        private function getGiftsByCode(param1:int) : ItemGiftBagInfo
        {
            return ItemConfig.instance.getConfig(param1) as ItemGiftBagInfo;
        }// end function

        public function getBagGiftGroup() : Vector.<LevelRewardGiftBag>
        {
            var level:*;
            var sortFunc:Function;
            var bag:LevelRewardGiftBag;
            var group:* = new Vector.<LevelRewardGiftBag>;
            var _loc_2:int = 0;
            var _loc_3:* = this._rewardDic;
            while (_loc_3 in _loc_2)
            {
                
                level = _loc_3[_loc_2];
                bag = new LevelRewardGiftBag();
                bag.level = level;
                bag.bag = this.getGiftsByCode(this.getCodeByLevel(level).giftCode);
                group.push(bag);
            }
            sortFunc = function (param1:LevelRewardGiftBag, param2:LevelRewardGiftBag) : Number
            {
                if (param1 == null || param1 == null)
                {
                    return 1;
                }
                if (param1.level > param2.level)
                {
                    return 1;
                }
                if (param1.level < param2.level)
                {
                    return -1;
                }
                return 0;
            }// end function
            ;
            group.sort(sortFunc);
            return group;
        }// end function

        public function get allClosedBetaGiftBagData() : Array
        {
            var _loc_1:* = ArrayUtil.DictAsArr(this._rewardDic);
            _loc_1.sortOn("level", Array.NUMERIC);
            return _loc_1;
        }// end function

        public function getAllLevelGiftsByCareer(param1:int) : Vector.<LevelRewardGiftDataGroup>
        {
            var level:*;
            var sortFunc:Function;
            var career:* = param1;
            var totalList:* = new Vector.<LevelRewardGiftDataGroup>;
            var _loc_3:int = 0;
            var _loc_4:* = this._rewardDic;
            while (_loc_4 in _loc_3)
            {
                
                level = _loc_4[_loc_3];
                totalList.push(this.getGiftsByLevelCareer(int(level), career));
            }
            sortFunc = function (param1:LevelRewardGiftDataGroup, param2:LevelRewardGiftDataGroup) : Number
            {
                if (param1 == null || param1 == null)
                {
                    return 1;
                }
                if (param1.level > param2.level)
                {
                    return 1;
                }
                if (param1.level < param2.level)
                {
                    return -1;
                }
                return 0;
            }// end function
            ;
            totalList.sort(sortFunc);
            return totalList;
        }// end function

        public function getGiftsByLevelCareer(param1:int, param2:int) : LevelRewardGiftDataGroup
        {
            var _loc_6:Vector.<LevelRewardDataItem> = null;
            var _loc_7:Vector.<LevelRewardDataItem> = null;
            var _loc_8:int = 0;
            var _loc_9:int = 0;
            var _loc_3:* = this.getCodeByLevel(param1);
            var _loc_4:* = this.getGiftsByCode(_loc_3.giftCode);
            var _loc_5:* = new LevelRewardGiftDataGroup();
            if (_loc_4)
            {
                _loc_6 = getGiftFromString(_loc_4.itemList1);
                _loc_7 = getGiftFromString(_loc_4.itemList2);
                _loc_8 = _loc_7.length;
                if (_loc_8 > 0)
                {
                    _loc_9 = 0;
                    while (_loc_9 < _loc_8)
                    {
                        
                        if (_loc_7[_loc_9].career == param2)
                        {
                            _loc_6.push(_loc_7[_loc_9]);
                        }
                        _loc_9++;
                    }
                }
                _loc_5.list = _loc_6;
                _loc_5.level = param1;
                _loc_5.receiveType = LevelRewardGiftDataGroup.Null;
            }
            return _loc_5;
        }// end function

        public static function get instance() : LevelRewardConfig
        {
            if (_instance == null)
            {
                _instance = new LevelRewardConfig;
            }
            return _instance;
        }// end function

        private static function getGiftFromString(param1:String) : Vector.<LevelRewardDataItem>
        {
            var _loc_3:Array = null;
            var _loc_4:Array = null;
            var _loc_5:int = 0;
            var _loc_6:LevelRewardDataItem = null;
            var _loc_2:* = new Vector.<LevelRewardDataItem>;
            if (param1 && param1 != "")
            {
                _loc_3 = param1.split("&");
                _loc_5 = 0;
                while (_loc_5 < _loc_3.length)
                {
                    
                    _loc_4 = String(_loc_3[_loc_5]).split("#");
                    _loc_6 = new LevelRewardDataItem();
                    _loc_6.type = int(_loc_4[0]);
                    _loc_6.id = int(_loc_4[1]);
                    _loc_6.num = int(_loc_4[2]);
                    _loc_6.career = int(_loc_4[3] ? (_loc_4[3]) : (0));
                    _loc_2.push(_loc_6);
                    _loc_5++;
                }
            }
            return _loc_2;
        }// end function

    }
}
